let requestUrl = null;

function addHandler() {
	requestUrl = "/xingyin_texiao_category/insert";

	$("#texiao_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#texiao_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/xingyin_texiao_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#type").combobox("setValue", rowData.type);
		$("#score").numberspinner("setValue", rowData.score);

		$("#texiao_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler(texiaoId) {
	$.messager.confirm("系统提示", "是否确认删除？", function(bool) {
		if (bool) {
			ajaxPost("/xingyin_texiao_category/deleteById", {
				id: texiaoId
			}, function(resp) {
				showMsg(resp.message);

				$("#skill_list").datagrid("reload");
			}, error);
		}
	});
}

function imageHandler() {
	let rowData = $("#texiao_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/xingyin_texiao_category/updateById";

		$("#texiao_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

$(document).ready(function() {

	$("#_type_").combobox({
		url: "/xingyin_texiao_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-特效类型-"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let type = $("#_type_").combobox("getValue");

		$("#texiao_list").datagrid("load", {
			type: type
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 120,
		required: true
	});

	$("#type").combobox({
		url: "/xingyin_texiao_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#score").numberspinner({
		min: 0,
		width: 120,
		required: true
	});

    $("#note").textbox({
		width: 314,
    	height: 100,
    	required: true,
		multiline: true
	});

	$("#texiao_dialog").dialog({
		title: "星印特效",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#texiao_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);

						$(selector).form("clear");
						$("#texiao_dialog").dialog("close");
						$("#texiao_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#texiao_dialog").dialog("close");
				$("#texiao_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	$("#upload").filebox({
		buttonText: "选择文件",
		width: 240,
		required: true,
		onChange: function() {
			let rowData = $("#texiao_list").datagrid("getSelected");

			if(rowData) {
				fileUpload(this, rowData.id, "/xingyin_texiao_category/upload");
			}
		}
	});

	$("#upload_dialog").dialog({
		title: "修改图片",
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#upload_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);
						resetValue("#upload");

						$(selector).form("clear");
						$("#upload_dialog").dialog("close");
						$("#texiao_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				resetValue("#upload");

				$("#upload_form").form("clear");
				$("#upload_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#texiao_list").datagrid({
		url: "/xingyin_texiao_category/selectByPage",
		height: height_table,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", title: "编号", align:"center"},
			{field: "name", title: "特效名称", align:"center", width: 50},
			{field: "type", title: "特效类型", align: "center", sortable: true, width: 50
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/xingyin_texiao_type/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#type_" + rowIndex).html(data.name);
						}, error);

						return "<div class='blue' id='type_" + rowIndex + "'></div>";
					}
				}
			},
			{field: "note", title: "特效说明", align:"center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "score", title: "特效评分", align:"center", width: 50},
			{field: "image", title: "特效图片", align: "center", width: 30
				, formatter: function(value) {
					return "<img alt='暂无图片' height='" + size + "' src='" + value + "' />";
				}
			},
			deleteColumn
		]]
	});

});